Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper method for reading/writing memory #123

Merged
merged 3 commits into from
Mar 16, 2023
Merged

Conversation

tenderlove
Copy link
Member

@tenderlove tenderlove commented Mar 15, 2023

This commit adds two new methods, Fiddle::Pointer.read and Fiddle::Pointer.write. Both methods take an address, and will read or write bytes at that address respectively.

For example we can read from an address without making a Pointer object:

Fiddle::Pointer.read(address, 5) # read 5 bytes

We can also write to an address without allocating a Pointer object:

Fiddle::Pointer.write(address, "bytes") # write 5 bytes

This allows us to read / write memory at arbitrary addresses without instantiating a new Fiddle::Pointer object.

Examples where this API would be useful 1 2 3

I also added a writer method for the same reasons as the reader.

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Fiddle::Pointer[value] and Fiddle::Pointer[address, length] may be confused.
How about Fiddle::Pointer.read/Fiddle::Pointer.write (like IO.read/IO.write) instead?

@tenderlove
Copy link
Member Author

Hmm. Fiddle::Pointer[value] and Fiddle::Pointer[address, length] may be confused.
How about Fiddle::Pointer.read/Fiddle::Pointer.write (like IO.read/IO.write) instead?

That sounds good to me. I wish we could use [] and []=, but I agree it's confusing.

This commit adds two new methods, `Fiddle::Pointer.read` and
`Fiddle::Pointer.write`.  Both methods take an address, and will read or
write bytes at that address respectively.

For example we can read from an address without making a Pointer object:

```ruby
Fiddle::Pointer.read(address, 5) # read 5 bytes
```

We can also write to an address without allocating a Pointer object:

```ruby
Fiddle::Pointer.write(address, "bytes") # write 5 bytes
```

This allows us to read / write memory at arbitrary addresses without
instantiating a new `Fiddle::Pointer` object.

Co-Authored-By: Sutou Kouhei <[email protected]>
@tenderlove tenderlove requested a review from kou March 16, 2023 00:35
Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

tenderlove and others added 2 commits March 16, 2023 08:46
@tenderlove tenderlove merged commit 04238ce into master Mar 16, 2023
@tenderlove tenderlove deleted the read-memory-helper branch March 16, 2023 15:52
matzbot pushed a commit to ruby/ruby that referenced this pull request Jun 28, 2023
(ruby/fiddle#123)

This commit adds two new methods, `Fiddle::Pointer.read` and
`Fiddle::Pointer.write`. Both methods take an address, and will read or
write bytes at that address respectively.

For example we can read from an address without making a Pointer object:

```ruby
Fiddle::Pointer.read(address, 5) # read 5 bytes
```

We can also write to an address without allocating a Pointer object:

```ruby
Fiddle::Pointer.write(address, "bytes") # write 5 bytes
```

This allows us to read / write memory at arbitrary addresses without
instantiating a new `Fiddle::Pointer` object.

Examples where this API would be useful
[1](https://github.com/tenderlove/tenderjit/blob/f03481d28bff4d248746e596929b0841de65f181/lib/tenderjit/fiddle_hacks.rb#L26-L28)
[2](https://github.com/tenderlove/ruby/blob/77c8daa2d40dd58eeb3785ce17dea2ee38f308d1/lib/ruby_vm/rjit/c_pointer.rb#L193)
[3](https://github.com/tenderlove/ruby/blob/77c8daa2d40dd58eeb3785ce17dea2ee38f308d1/lib/ruby_vm/rjit/c_pointer.rb#L284)

I also added a writer method for the same reasons as the reader.

---------

ruby/fiddle@04238cefed

Co-authored-by: Sutou Kouhei <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants